<?php
/**
 * 频道焦造型日历
 * showType 
 * calendarcontroller.php
 * Create on 2015-04-15 16:45:24
 * Create by yuebin
 */
namespace haibao\cms\business\control\pc\channel\fashion;

use haibao\cms\business\control\ArticleController;
use haibao\cms\business\control\PublicController as PublicControllerBusiness;

class Calendarcontroller extends \haibao\cms\business\control\pc\channel\ChannelController{

	public function __construct($updateTime = null,$controlId,$isPublish){
		parent::__construct($updateTime,$controlId,$isPublish);
	}
	
	/**
	 * 显示模版内容（模版动态数据的绑定和模版输出）
	 * @param : String $content
	 * @param : Array $controlData;
	 */
	public function displayTemplateHtml(){

		$controlData = $this->getParameter(self::$CONTROL_DATA);
		$content = $this->getParameter(self::$CONTROL_CONTENT);
		$parameters = array();
		if($controlData['auto']){
				
			$temp = array();
			foreach($controlData['auto'] as $data){

				$parameterValue = json_decode($data['ParameterValue'],true);
				$parameterValue['isRecommend'] = 1;
				array_push($parameters,$parameterValue);
			}
		}
		$this->getCalendarData();
		$this->getProductData($controlData['manual']['limit'],$parameters);
		
		$path = $this->generateHtmlPath(__CLASS__);
		$isNewHtml = $this->generateHtml($path,$content);
		$content = $this->getHtml($path);
		$content = $this->replaceCDNLink($content);
		return $content;
	}
	
	private function getCalendarData(){
		
		$calendar = PublicControllerBusiness::getNewCalendarInfo();
		$article = '';
		if($calendar){
			
			// DO : 获取造型日历关联的文章信息
			$articleControl = ArticleController::initArticleControl();
			$article = $articleControl->articleData->getOneById($calendar->ArticleId);
			$proImage = $articleControl->getArticleProImage((array)$calendar->ArticleId, \haibao\cms\model\data\ArticleProImage::Level_SIX);
			if(isset($proImage[$article->Id])){
				
				if(isset($proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX])){
					$article->ArticleProImage = $proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_SIX];
				}else{
					$article->ArticleProImage = $proImage[$article->Id][\haibao\cms\model\data\ArticleProImage::Level_ZERO];
				}
			}else{
				$article->ArticleProImage = '';
			}
		}
		$this->assign('article', $article);
		$this->assign('calendar', $calendar);
		$this->assign('calendarHost', \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_CALENDAR));
	}
	/**
	 * @param : $dataCondition 数据条件
	 * @param : $parameterValue 手动数据
	 */
	public function getProductData($limit,$parameters){
		$paramNum = count($parameters);
		if($paramNum < $limit){
			$diffLimit = $limit - $paramNum;
			$products = PublicControllerBusiness::getRandProductList($diffLimit);
			foreach($products as $product){
				$temp =array();
				$temp['title'] = $product->ProductName;
				$temp['img'] = $product->ImageUrl;
				$temp['url'] = \haibao\classlibrary\cms\Config::getConfig(\haibao\classlibrary\cms\Config::CLASSLIBRARY_CONFIG_HOST_BRANDS).'/product/'.$product->Id.'/';
				$temp['isRecommend'] = 0;
				array_push($parameters,$temp);
			}
		}else{
			$parameters = array_slice($parameters, 0,$limit);
		}
		$this->assign('parameters',$parameters);
	}
	

}